package com.maochang.config.etc;

import com.maochang.controller.fitter.SystemLogoutFilter;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;


@Configuration //配置类
public class ShrioConfig {
    @Resource
    private  BbReam ream;


    //1.创建安全管理器
    @Bean
    public DefaultWebSecurityManager  securityManager(){
        DefaultWebSecurityManager defaultSecurityManager=new DefaultWebSecurityManager();
        //把session  给还原回去    那等于没用
        //defaultSecurityManager.setSessionManager(new DefaultSessionManager());
        /**
         *   采用 shiroFilter   作用 1：就是让 shiro  和  servlet 容器产生关联
         *                             产生关联的好处:shiro 的会话 和 servlet 的  会话能同步
         *                             我们可以使用 内置的过滤器
         */
        //把凭证匹配器添加给ream
        ream.setCredentialsMatcher(credentialsMatcher());
        defaultSecurityManager.setRealm(ream);
        return defaultSecurityManager;
    }
    ////2.spring  容器  初始化完毕后 执行的代码
    //下面设置过所以不要了
    //@PostConstruct
    //public void  init(){
    //    //把安全管理器设置到  shiro 中
    //    SecurityUtils.setSecurityManager(securityManager());
    //}
    //3.创建凭证匹配器
    public HashedCredentialsMatcher credentialsMatcher(){
        HashedCredentialsMatcher credentialsMatcher=new HashedCredentialsMatcher();
        //设置加密方式
        credentialsMatcher.setHashAlgorithmName("md5");
        //设置加密次数
        credentialsMatcher.setHashIterations(1);
        return  credentialsMatcher;
    }
    //4.shirofilter
    @Bean(name ="shiroFilter")
    public ShiroFilterFactoryBean  shiroFilterFactoryBean(){
        ShiroFilterFactoryBean  shiroFilterFactoryBean=new ShiroFilterFactoryBean();
        //设置filter 的安全管理器
        shiroFilterFactoryBean.setSecurityManager(securityManager());  //默认 shiro  和web 容器没有关系的
        //配置过滤器链
        Map<String, String> filterMap = new LinkedHashMap<>();
        //匿名过滤器
        filterMap.put("/login", "anon");
        filterMap.put("/webjars/**", "anon");
        filterMap.put("WEB-INF/static/**", "anon");
        filterMap.put("/getAuthCodeImg", "anon");
        filterMap.put("/static/**","anon");
        filterMap.put("/Menu/**","anon");
        filterMap.put("/ac/**","anon");
        //退出过滤器
        filterMap.put("/logout", "logout");
        filterMap.put("/aa/**","anon");
        //其他都需要授权控制  鉴权
        //filterMap.put("/**","authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        //如果没有登录就跳转到这个地址
        shiroFilterFactoryBean.setLoginUrl("/login");
        shiroFilterFactoryBean.setFilters(logoutFilter());
        return  shiroFilterFactoryBean;
    }
    //5.过滤器其他配置
    //@Bean
    //public DefaultShiroFilterChainDefinition  shiroFilterChainDefinition(){
    //    DefaultShiroFilterChainDefinition aa=new DefaultShiroFilterChainDefinition();
    //    Map<String, String> filterChainDefinitionMap=aa.getFilterChainMap();
    //    aa.addPathDefinition("/login","anon");
    //    aa.addPathDefinition("/webjars/**","anon");
    //    aa.addPathDefinition("WEB-INF/static/**","anon");
    //    aa.addPathDefinition("/logout","logout");
    //    aa.addPathDefinition("/**","authc");
    //    return aa;
    //}
    //自定义过滤器
    @Bean
    public Map logoutFilter(){
        SystemLogoutFilter aa=new SystemLogoutFilter();
        aa.setRedirectUrl("/login");
        Map a=new HashMap();
        a.put("logout",aa);
        return  a;
    };
}
